home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Frontier 4.0.1 / Frontier SDK 4.0b2 / Sample Code / OutlineSharer / main.c next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  13.5 KB  |  755 lines  |  [TEXT/CWIE]

  1.  
  2. /*© copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
  3.  
  4.  
  5. /*
  6. Briefly -- this is a very small program that demonstrates the UserLand Outline Sharing
  7. protocol. For detailed info, see the enclosed "About Outline Sharing" file.
  8.  
  9. Please contact UserLand Software at on AppleLink at the UserLand Discussion board under
  10. the Third Parties icon, or GO USERLAND on CompuServe for further information on outline 
  11. sharing. 
  12.  
  13. We hope you like outline sharing and add it to your Macintosh application.
  14.  
  15. Thanks!
  16.  
  17. UserLand Software
  18.  
  19. 7/1/92 DW: Added support for system event handlers.
  20. */
  21.  
  22.  
  23. #include <menusharing.h>
  24. #include <iac.h>
  25. #include "outlinesharing.h"
  26.  
  27.  
  28. #define applemenu 128 /*the resource id of the apple menu*/
  29.  
  30. #define aboutitem 1 /*the about command*/
  31.  
  32. #define filemenu 129 /*resource id of file menu, shared menus appear to right of this*/
  33.  
  34. #define firstsharedmenu filemenu+1 /*resource id for first shared menu*/
  35.  
  36. #define quititem 1 /*the single item in the file menu -- this is a very simple program!*/
  37.  
  38. MenuHandle happlemenu, hfilemenu; /*the two fixed, non-shared menus in this program*/
  39.  
  40. WindowPtr mainwindow = nil; /*the menu sharing test window*/
  41.  
  42. Str255 windowmessage; /*the message that's displayed in the main window*/
  43.  
  44. short flexitmainloop = false; /*when true we fall thru the main event loop*/
  45.     
  46.     
  47.  
  48.  
  49. static void copystring (Str255 source, Str255 dest) {
  50.  
  51.     /*
  52.     create a copy of source in dest.  copy the length byte and
  53.     all the characters in the source string.
  54.  
  55.     assume the strings are pascal strings, with the length byte in
  56.     the first character of the string.
  57.     */
  58.  
  59.     register short i, len;
  60.     
  61.     len = (short) source [0];
  62.     
  63.     for (i = 0; i <= len; i++) 
  64.         dest [i] = source [i];
  65.     } /*copystring*/
  66.  
  67.  
  68. static void ellipsize (Str255 s, short width) {
  69.  
  70.     /*
  71.     if the string fits inside the given number of pixels, fine -- do nothing
  72.     and return.
  73.     
  74.     if not, return a string that does fit, with ellipses representing the 
  75.     deleted characters.  ellipses are generated by pressing option-semicolon.
  76.     */
  77.     
  78.     register char len;
  79.     register short newwidth;
  80.     
  81.     if ((newwidth = StringWidth (s)) <= width) /*nothing to do, the string fits*/
  82.         return;
  83.     
  84.     len = s [0]; /* current length in characters*/
  85.     
  86.     width -= CharWidth ('…'); /* subtract width of ellipses*/
  87.         
  88.     do { /*until it fits (or we run out of characters)*/
  89.     
  90.         newwidth -= CharWidth (s [len]);
  91.         
  92.         --len;
  93.     } while ((newwidth > width) && (len != 0));
  94.     
  95.     ++len; /*make room for the ellipses*/
  96.     
  97.     s [len] = '…'; 
  98.     
  99.     s [0] = (char) len;
  100.     } /*ellipsize*/
  101.  
  102.  
  103. static void centerstring (Rect r, Str255 s) {
  104.     
  105.     /*
  106.     draw the string in the current font, size and style, centered inside
  107.     the indicated rectangle.
  108.     */
  109.     
  110.     register short rh = r.bottom - r.top;
  111.     register short rw = r.right - r.left;
  112.     register short h, v;
  113.     FontInfo fi;
  114.     
  115.     GetFontInfo (&fi);
  116.     
  117.     ellipsize (s, rw); /*make sure it fits inside the rectangle, width-wise*/
  118.     
  119.     h = r.left + ((rw - StringWidth (s)) / 2);
  120.     
  121.     v = r.top + ((rh - (fi.ascent + fi.descent)) / 2) + fi.ascent;
  122.     
  123.     MoveTo (h, v);
  124.     
  125.     ClipRect (&r);
  126.     
  127.     DrawString (s);
  128.     } /*centerstring*/
  129.  
  130.  
  131. static void setfontsizestyle (short fontnum, short fontsize, short fontstyle) {
  132.  
  133.     TextFont (fontnum);
  134.     
  135.     TextSize (fontsize);
  136.     
  137.     TextFace (fontstyle);
  138.     } /*setfontsizestyle*/
  139.     
  140.     
  141. static void updatemainwindow (void) {
  142.     
  143.     Rect r;
  144.     Str255 s;
  145.     
  146.     r = (*mainwindow).portRect;
  147.     
  148.     EraseRect (&r);
  149.     
  150.     setfontsizestyle (helvetica, 12, 0);
  151.     
  152.     centerstring (r, windowmessage);
  153.     
  154.     NumToString (FreeMem () / 1024, s);
  155.     
  156.     MoveTo (r.left + 3, r.bottom - 3);
  157.     
  158.     setfontsizestyle (geneva, 9, 0);
  159.     
  160.     DrawString (s);
  161.     
  162.     DrawString ("\pK");
  163.     } /*updatemainwindow*/
  164.     
  165.     
  166. static Boolean setwindowmessage (Str255 s) {
  167.     
  168.     copystring (s, windowmessage);
  169.     
  170.     SetPort (mainwindow);
  171.     
  172.     updatemainwindow ();
  173.     
  174.     return (true);
  175.     } /*setwindowmessage*/
  176.  
  177.  
  178. static Boolean initmainwindow (void) {
  179.     
  180.     register WindowPtr w;
  181.     
  182.     w = mainwindow = GetNewWindow (128, nil, (WindowPtr) -1);
  183.     
  184.     if (w == nil)
  185.         return (false);
  186.     
  187.     ShowWindow (w);
  188.     
  189.     return (true);
  190.     } /*initmainwindow*/
  191.  
  192.  
  193. static void handleupdate (EventRecord *ev) {
  194.     
  195.     register WindowPtr w;
  196.     
  197.     w = (WindowPtr) (*ev).message;
  198.     
  199.     BeginUpdate (w);
  200.     
  201.     SetPort (w);
  202.     
  203.     updatemainwindow ();
  204.     
  205.     EndUpdate (w);
  206.     } /*handleupdate*/
  207.  
  208.  
  209. static void handledrag (EventRecord *ev, WindowPtr w) {
  210.     
  211.     Rect r;
  212.  
  213.     r = qd.screenBits.bounds; 
  214.     
  215.     r.top = r.top + GetMBarHeight (); 
  216.     
  217.     InsetRect (&r, 4, 4);
  218.     
  219.     DragWindow (w, (*ev).where, &r);
  220.     } /*handledrag*/
  221.  
  222.  
  223. static void handlemenu (long codeword) {
  224.     
  225.     register short idmenu, iditem;
  226.     
  227.     iditem = LoWord (codeword);
  228.     
  229.     idmenu = HiWord (codeword);
  230.     
  231.     if (SharedMenuHit (idmenu, iditem))         
  232.         goto exit;
  233.     
  234.     switch (idmenu) {
  235.     
  236.         case applemenu: 
  237.             switch (iditem) {
  238.                 
  239.                 case aboutitem:
  240.                     Alert (262, nil);
  241.                     
  242.                     break;
  243.             
  244.                 default: {
  245.                 
  246.                     Str255 s;
  247.                     
  248.                     GetItem (happlemenu, iditem, s);
  249.                     
  250.                     OpenDeskAcc (s);
  251.                     
  252.                     break;
  253.                     }
  254.                 } /*switch*/
  255.             
  256.             break; /*apple menu*/
  257.  
  258.         case filemenu: 
  259.             switch (iditem) {
  260.                 
  261.                 case quititem:
  262.                 
  263.                     flexitmainloop = true;
  264.                     
  265.                     break;
  266.                 } /*switch*/
  267.             
  268.             break; /*file menu*/
  269.             
  270.         } /*switching on which menu was invoked*/
  271.         
  272.     exit:
  273.     
  274.     HiliteMenu (0);
  275.     } /*handlemenu*/
  276.  
  277.  
  278. static void handlemouse (EventRecord *ev) {
  279.  
  280.     register short part;
  281.     WindowPtr w;
  282.     
  283.     part = FindWindow ((*ev).where, &w);
  284.     
  285.     if (w != nil) 
  286.     
  287.         if (w != FrontWindow ()) { /*just like all other Mac programs*/
  288.             
  289.             SelectWindow (w);
  290.                             
  291.             return; /*the mouse click is consumed by the bringtofront operation*/
  292.             }
  293.     
  294.     switch (part) {
  295.     
  296.         case inMenuBar: 
  297.             handlemenu (MenuSelect ((*ev).where)); 
  298.             
  299.             break;
  300.         
  301.         case inSysWindow:
  302.             SystemClick (ev, w); 
  303.             
  304.             break;
  305.         
  306.         case inDrag:
  307.             handledrag (ev, w);
  308.             
  309.             break;
  310.             
  311.         } /*switch*/
  312.     } /*handlemouse*/
  313.  
  314.  
  315. static void handlekeystroke (EventRecord *ev) {
  316.  
  317.     register char ch = (*ev).message & charCodeMask;
  318.     
  319.     if (SharedScriptRunning ()) { /*cmd-period terminates the script*/
  320.     
  321.         if (((*ev).modifiers & cmdKey) && (ch == '.')) { 
  322.             
  323.             CancelSharedScript (); /*cancel the shared menu script, if one is running*/
  324.         
  325.             return;
  326.             }
  327.         }
  328.     
  329.     handlemenu (MenuKey (ch)); /*not cmd-period, process the normal way*/
  330.     } /*handlekeystroke*/
  331.  
  332.  
  333. static void handleevent (EventRecord *ev) {
  334.     
  335.     switch ((*ev).what) {
  336.     
  337.         case keyDown: case autoKey: 
  338.             handlekeystroke (ev);
  339.             
  340.             break;
  341.             
  342.         case mouseDown:
  343.             handlemouse (ev);
  344.             
  345.             break;
  346.         
  347.         case updateEvt:
  348.             handleupdate (ev);
  349.             
  350.             break;
  351.             
  352.         case kHighLevelEvent:
  353.             AEProcessAppleEvent (ev);
  354.             
  355.             break;
  356.         } /*switch*/
  357.     } /*handleevent*/
  358.  
  359.  
  360. static void maineventloop (void) {
  361.     
  362.     EventRecord ev;
  363.     
  364.     while (!flexitmainloop) {
  365.         
  366.         if (WaitNextEvent (everyEvent, &ev, 1, nil)) 
  367.             handleevent (&ev);
  368.         
  369.         CheckSharedMenus (firstsharedmenu); 
  370.         } /*while*/
  371.     } /*maineventloop*/
  372.  
  373.  
  374. static void initmenus (void) {
  375.     
  376.     /*
  377.     set up our apple and file menus.  nothing fancy.
  378.     */
  379.     
  380.     happlemenu = GetMenu (applemenu); 
  381.     
  382.     AddResMenu (happlemenu, 'DRVR'); 
  383.     
  384.     InsertMenu (happlemenu, 0); 
  385.     
  386.     hfilemenu = GetMenu (filemenu); 
  387.     
  388.     InsertMenu (hfilemenu, 0);
  389.     
  390.     DrawMenuBar ();
  391.     } /*initmenus*/
  392.  
  393.  
  394. static void initmacintosh (void) {
  395.  
  396.     /*
  397.     the magic stuff that every Macintosh application needs to do 
  398.     before doing anything else.
  399.     */
  400.  
  401.     register short i;
  402.         
  403.     MaxApplZone ();
  404.     
  405.     for (i = 0; i < 10; i++) 
  406.         MoreMasters ();
  407.     
  408.     InitGraf (&qd.thePort);
  409.     
  410.     InitFonts ();
  411.     
  412.     FlushEvents (everyEvent, 0);
  413.     
  414.     InitWindows ();
  415.     
  416.     InitMenus ();
  417.     
  418.     TEInit ();
  419.     
  420.     InitDialogs (0L);
  421.     
  422.     InitCursor ();
  423.     
  424.     for (i = 0; i < 5; i++) { /*register with Multifinder*/
  425.         
  426.         EventRecord ev;
  427.         
  428.         EventAvail (everyEvent, &ev); /*see TN180 -- splash screen*/
  429.         } /*for*/
  430.     } /*initmacintosh*/
  431.  
  432.  
  433. static pascal OSErr handlequit (AppleEvent *event, AppleEvent *reply, long refcon) {
  434.     
  435.     /*DebugStr ("\pquitapp");*/
  436.     
  437.     flexitmainloop = true;
  438.     
  439.     return (noErr);
  440.     } /*handlequit*/
  441.     
  442.     
  443. static pascal OSErr handleopenapp (AppleEvent *event, AppleEvent *reply, long refcon) {
  444.     
  445.     /*DebugStr ("\popenapp");*/
  446.     
  447.     return (noErr);
  448.     } /*handleopenapp*/
  449.  
  450.  
  451. static pascal Boolean openfilespec (FSSpec *fs) {
  452.  
  453.     /*DebugStr ((*fs).name);*/
  454.     
  455.     return (true);
  456.     } /*openfilespec*/
  457.     
  458.  
  459. static pascal Boolean printfilespec (FSSpec *fs) {
  460.  
  461.     /*DebugStr ((*fs).name);*/
  462.     
  463.     return (true);
  464.     } /*printfilespec*/
  465.     
  466.  
  467. static pascal OSErr handleopen (AppleEvent *event, AppleEvent *reply, long refcon) {
  468.     
  469.     IACglobals.event = event;
  470.     
  471.     IACglobals.reply = reply;
  472.     
  473.     IACglobals.refcon = refcon;
  474.     
  475.     return (IACdrivefilelist (&openfilespec));
  476.     } /*handleopen*/
  477.     
  478.         
  479. static pascal OSErr handleprint (AppleEvent *event, AppleEvent *reply, long refcon) {
  480.     
  481.     IACglobals.event = event;
  482.     
  483.     IACglobals.reply = reply;
  484.     
  485.     IACglobals.refcon = refcon;
  486.     
  487.     return (IACdrivefilelist (&printfilespec));
  488.     } /*handleprint*/
  489.     
  490.         
  491. static pascal OSErr setmessageverb (AppleEvent *event, AppleEvent *reply, long refcon) {
  492.     
  493.     /*
  494.     display a message in OutlineSharer's window.
  495.     */
  496.  
  497.     Str255 s;
  498.     
  499.     if (SharedScriptCancelled (event, reply)) 
  500.         return (noErr);
  501.     
  502.     IACglobals.event = event;
  503.     
  504.     IACglobals.reply = reply;
  505.     
  506.     if (!IACgetstringparam ((OSType) keyDirectObject, s))
  507.         return (noErr);
  508.     
  509.     IACreturnboolean (setwindowmessage (s));
  510.     
  511.     return (noErr);
  512.     } /*setmessageverb*/
  513.     
  514.  
  515. static pascal OSErr setoutlineverb (AppleEvent *event, AppleEvent *reply, long refcon) {
  516.     
  517.     /*
  518.     set the current outline from the outline parameter passed as part
  519.     of the Apple Event.
  520.     */
  521.  
  522.     hdloutlinerecord houtline;
  523.         
  524.     if (SharedScriptCancelled (event, reply)) 
  525.         return (noErr);
  526.     
  527.     IACglobals.event = event;
  528.     
  529.     IACglobals.reply = reply;
  530.     
  531.     if (!IACgetoutlineparam ((OSType) keyDirectObject, &houtline))
  532.         return (noErr);
  533.         
  534.     opDisposeOutlineRecord (outlinedata); /*only one outline managed at a time*/
  535.     
  536.     outlinedata = houtline; /*set the current outline*/
  537.     
  538.     IACreturnboolean (true);
  539.     
  540.     return (noErr);
  541.     } /*setoutlineverb*/
  542.     
  543.     
  544. static Boolean nooutlineerror (void) {
  545.  
  546.     if (outlinedata == nil) {
  547.         
  548.         IACreturnerror (1, "\pNo outline has been sent to OutlineSharer");
  549.         
  550.         return (true);
  551.         }
  552.     
  553.     return (false);
  554.     } /*nooutlineerror*/
  555.     
  556.     
  557. static pascal OSErr getoutlineverb (AppleEvent *event, AppleEvent *reply, long refcon) {
  558.     
  559.     /*
  560.     return the outline that's resident in OutlineSharer.
  561.     */
  562.     
  563.     if (SharedScriptCancelled (event, reply)) 
  564.         return (noErr);
  565.     
  566.     IACglobals.event = event;
  567.     
  568.     IACglobals.reply = reply;
  569.     
  570.     if (nooutlineerror ()) 
  571.         return (noErr);
  572.     
  573.     IACreturnoutline (outlinedata);
  574.     
  575.     return (noErr);
  576.     } /*getoutlineverb*/
  577.     
  578.     
  579. static void allupper (Str255 bs) {
  580.     
  581.     register char len = bs [0];
  582.     register char *p = (char *) &bs [1];
  583.     register char ch;
  584.     
  585.     while (len--) {
  586.         
  587.         ch = *p;
  588.         
  589.         if ((ch >= 'a') && (ch <= 'z'))
  590.             *p -= 32;
  591.             
  592.         p++;
  593.         } /*while*/
  594.     } /*allupper*/
  595.     
  596.     
  597. static Boolean uppercasevisit (hdlheadrecord hnode) {
  598.     
  599.     Str255 bs;
  600.     
  601.     opGetHeadString (hnode, bs);
  602.     
  603.     allupper (bs);
  604.     
  605.     opSetHeadString (hnode, bs);
  606.     
  607.     return (true);
  608.     } /*uppercasevisit*/
  609.     
  610.     
  611. static pascal OSErr uppercaseoutlineverb (AppleEvent *event, AppleEvent *reply, long refcon) {
  612.  
  613.     /*
  614.     convert all the characters in the current outline to lowercase.
  615.     */
  616.     
  617.     if (SharedScriptCancelled (event, reply)) /*part of the menu sharing protocol*/
  618.         return (noErr);
  619.     
  620.     IACglobals.event = event;
  621.     
  622.     IACglobals.reply = reply;
  623.     
  624.     if (nooutlineerror ()) /*no outline to uppercase*/
  625.         return (noErr);
  626.     
  627.     opVisitOutline (uppercasevisit); /*visit every node in the outline*/
  628.     
  629.     IACreturnboolean (true);
  630.     
  631.     return (noErr);
  632.     } /*uppercaseoutlineverb*/
  633.     
  634.     
  635. static void alllower (Str255 bs) {
  636.     
  637.     register char len = bs [0];
  638.     register char *p = (char *) &bs [1];
  639.     register char ch;
  640.     
  641.     while (len--) {
  642.         
  643.         ch = *p;
  644.         
  645.         if ((ch >= 'A') && (ch <= 'Z'))
  646.             *p += 32;
  647.             
  648.         p++;
  649.         } /*while*/
  650.     } /*alllower*/
  651.     
  652.     
  653. static Boolean lowercasevisit (hdlheadrecord hnode) {
  654.     
  655.     Str255 bs;
  656.     
  657.     opGetHeadString (hnode, bs);
  658.     
  659.     alllower (bs);
  660.     
  661.     opSetHeadString (hnode, bs);
  662.     
  663.     return (true);
  664.     } /*lowercasevisit*/
  665.     
  666.     
  667. static pascal OSErr lowercaseoutlineverb (AppleEvent *event, AppleEvent *reply, long refcon) {
  668.     
  669.     /*
  670.     convert all the characters in the current outline to lowercase.
  671.     */
  672.     
  673.     if (SharedScriptCancelled (event, reply)) /*part of the menu sharing protocol*/
  674.         return (noErr);
  675.     
  676.     IACglobals.event = event;
  677.     
  678.     IACglobals.reply = reply;
  679.     
  680.     if (nooutlineerror ()) /*no outline to lowercase*/
  681.         return (noErr);
  682.         
  683.     opVisitOutline (lowercasevisit); /*visit every node in the outline*/
  684.     
  685.     IACreturnboolean (true);
  686.     
  687.     return (noErr);
  688.     } /*lowercaseoutlineverb*/
  689.     
  690.     
  691. static pascal void errordialog (Str255 s) {
  692.     
  693.     ParamText (s, "\p", "\p", "\p"); 
  694.     
  695.     Alert (263, nil);
  696.     } /*errordialog*/
  697.  
  698.  
  699. static pascal void eventfilter (EventRecord *ev) {
  700.     
  701.     handleevent (ev);
  702.     } /*eventfilter*/
  703.     
  704.  
  705. void main (void) {
  706.     
  707.     initmacintosh ();
  708.     
  709.     if (!InitSharedMenus (&errordialog, &eventfilter))
  710.         goto error;
  711.     
  712.     if (!IACinstallhandler ('OUTS', 'smsg', (ProcPtr) setmessageverb))
  713.         goto error;
  714.     
  715.     if (!IACinstallsystemhandler ('outs', 'sout', (ProcPtr) setoutlineverb))
  716.         goto error;
  717.     
  718.     if (!IACinstallsystemhandler ('outs', 'gout', (ProcPtr) getoutlineverb))
  719.         goto error;
  720.     
  721.     if (!IACinstallsystemhandler ('outs', 'lcas', (ProcPtr) lowercaseoutlineverb))
  722.         goto error;
  723.     
  724.     if (!IACinstallsystemhandler ('outs', 'ucas', (ProcPtr) uppercaseoutlineverb))
  725.         goto error;
  726.     
  727.     if (!IACinstallhandler (kCoreEventClass, kAEOpenApplication, (ProcPtr) &handleopenapp))
  728.         goto error;
  729.     
  730.     if (!IACinstallhandler (kCoreEventClass, kAEOpenDocuments, (ProcPtr) &handleopen))
  731.         goto error;
  732.     
  733.     if (!IACinstallhandler (kCoreEventClass, kAEPrintDocuments, (ProcPtr) &handleprint))
  734.         goto error;
  735.     
  736.     if (!IACinstallhandler (kCoreEventClass, kAEQuitApplication, (ProcPtr) &handlequit))
  737.         goto error;
  738.  
  739.     initmenus ();
  740.     
  741.     initmainwindow ();
  742.     
  743.     maineventloop ();
  744.         
  745.     ExitToShell ();
  746.     
  747.     error:
  748.     
  749.     Alert (261, nil); /*this application requires system 7.0 or higher*/
  750.         
  751.     ExitToShell ();
  752.     } /*main*/
  753.  
  754.  
  755.